# 0. 导包
import unittest
import requests
import time
from parameterized.parameterized import parameterized
from app import BASE_URL
from base.base_excel import read_excel


class TestAPI(unittest.TestCase):
    # activity_name = "测试活动" + str(random.randint(100000000000, 00000000000000))
    headers = {'Content-Type': "application/x-www-form-urlencoded",
               "token": "285190856c62416897ba2b2ffdce3d00",
               "appid": "wx6ee906fbaa89d8b0",
               "invokepoolcode": "activity_mini_app"
               }

    def test_activity_list(self):
        """"access_token": "5ffc11360c6e4d4e961e8123c3661803"
        活动首页报名列表
        :return:
        """
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "/vega-server/springActivitySign/querySignList"
        data = {
            "id": "104",
            "pageNumber": "1",
            "pageSize": "5",
            "userid": "136981",
                }
        # data["name"] = activity_name
        # 2. 设置提交数据,发送请求
        # 查询所有 无请求数据
        response = requests.get(url, headers=self.headers, params=data)
        response.encoding = "utf-8"
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)

    def test_knot_detail(self):
        """
        查询中国结明细
        :return:
        """
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "vega-server/activity/findKnotDetail"
        # 2. 设置提交数据,发送请求
        data = {
            "userId": "136981",
        }
        # 查询所有 无请求数据
        response = requests.get(url, headers=self.headers, params=data, verify=False)
        response.encoding = "utf-8"
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)

    def test_find_activity(self):
        """
        查询活动攻略和活动说明
        :return:
        """
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "vega-server/activity/findActivity"
        # 2. 设置提交数据,发送请求
        data = {"activityId": "104"}

        # 查询所有 无请求数据
        response = requests.get(url, headers=self.headers, params=data, verify=False)
        response.encoding = "utf-8"
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)

    def test_insert_ticket(self):
        """
        投票
        :return:
        """
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "vega-server/activity/insertTicket"
        # 2. 设置提交数据,发送请求
        data = {"registrationId": 164,
                "userId": 136981,
                "phone": 15347099941,
                "oldUserId": 136981}
        response = requests.get(url, headers=self.headers, params=data)
        response.encoding = "utf-8"
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.text
        print(text)

    def test_share(self):
        """
        分享得中国结
        :return:
        """
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "vega-server/activity/insertShare"
        # 2. 设置提交数据,发送请求
        data = {
            "userid": "136981",
            "activityid": "104"
        }
        # 查询所有 无请求数据
        response = requests.get(url, headers=self.headers, params=data, verify=False)
        response.encoding = "utf-8"
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)

    def test_task_list(self):
        """
        查询任务列表
        :return:
        """
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "vega-server/activity/findTask"
        # 2. 设置提交数据,发送请求
        data = {"userId": 136981}
        response = requests.get(url, headers=self.headers, params=data, verify=False)
        response.encoding = "utf-8"
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)

    def test_get_activity(self):
        """
        获取活动信息
        :return:
        """
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "vega-server/springActivity/query"
        # 2. 设置提交数据,发送请求, id 活动ID
        data = {
            "id": "134297"}
        # 查询所有 无请求数据
        response = requests.get(url, headers=self.headers, params=data, verify=False)
        response.encoding = "utf-8"
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)

    def test_leader_board(self):
        """
        查询中国结排行榜
        :return:
        """
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "vega-server/activity/findUserLeaderBoard"
        # 2. 设置提交数据,发送请求
        data = "activityId=104&userId=136981"

        # 查询所有 无请求数据
        response = requests.post(url, headers=self.headers,
                                 data=data, verify=False)
        response.encoding = "utf-8"
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)

    # @parameterized.expand(read_excel("test_data.xlsx", "toupiao"))
    def test_add_knot(self):
        """
        添加中国结
        :return:
        """

        ac_type = -1
        num = 999
        userid = 136981
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "vega-server/activity/addKnot"
        # 2. 设置提交数据,发送请求
        data = "type={}&num={}&userId={}".format(ac_type, num, userid)
        response = requests.post(url, headers=self.headers,
                                 data=data, verify=False)
        response.encoding = "utf-8"
        # 查询所有 无请求数据
        # 3 查看响应结果
        # 3.1 查看状态码
        # code = response.status_code
        # print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)


    def test_get_goods(self):
        """
        兑换礼品
        :return:
        """

        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "vega-server/activity/updateGoods"
        # 2. 设置提交数据,发送请求
        data = "userId=136981&activityId=104&goodId=18"
        response = requests.post(url, headers=self.headers,
                                 data=data, verify=False)
        response.encoding = "utf-8"
        # 查询所有 无请求数据
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)


    def test_sign(self):
        """
        用户签到
        :return:
        """
        # 1. 设置资源路径 - 定位接口资源
        url = BASE_URL + "/vega-server/springActivitySignIn/sign"
        # 2. 设置提交数据,发送请求
        data = "userId=136981"
        response = requests.post(url, headers=self.headers,
                                 data=data, verify=False)
        response.encoding = "utf-8"
        # 3 查看响应结果
        # 3.1 查看状态码
        code = response.status_code
        print(code)
        # 3.2 查看返回结果文本格式  字符串
        text = response.json()
        print(text)
